/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 */
package top.hiai._admin.article;

import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import top.hiai.common.account.UserService;
import top.hiai.common.model.Article;
import top.hiai.common.model.Information;
import top.hiai.index.IndexService;
import top.hiai.my.article.MyArticleService;
import top.hiai.my.favorite.MyFavoriteService;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top
* @Introduction 类的介绍：后台文章Service
*/

public class ArticleAdminService {
	
	public static final ArticleAdminService me = new ArticleAdminService();
	
	private Article articleDao = new Article().dao();

	/**
	 * 后台管理所有文章分页
	 */
	public Page<Article> paginate(int pageNum,int pageSize) {
		Page<Article> articlePage = articleDao.paginate(pageNum, pageSize, "select *", "from bbs_article order by addtime desc");
		UserService.me.join("uid", articlePage.getList(), "nickName", "avatar");
		return articlePage;
	}
	
	/**
	 * 锁定文章,就是不让用户查看
	 * @param aid
	 * @return
	 */
	public Ret lock(int aid) {
		int n = Db.update("update bbs_article set status = ? where aid=?", Article.STATUS_OK, aid);
		if (n > 0) {
			return Ret.ok("msg", "显示成功");
		} else {
			return Ret.fail().set("msg", "锁定失败");
		}
	}

	/**
	 * 解锁文章，就是让用户查看
	 * @param aid
	 * @return
	 */
	public Ret unlock(int aid) {
		int n = Db.update("update bbs_article set status = ? where aid = ?", Article.STATUS_NOT , aid);
		if (n > 0) {
			return Ret.ok("msg", "解锁成功");
		} else {
			return Ret.fail().set("msg", "解锁失败");
		}
	}
	/**
	 * 删除文章
	 * 删除 文章之前，要删除用户收藏的记录,同时删除文章评论表
	 * @param
	 */
	public void articleDelete(int aid){
		//先获取文章的发表者ID
		Integer accountId = Db.queryInt("select uid from bbs_article where aid=? limit 1", aid);
		Db.tx(new IAtom() {
			public boolean run() throws SQLException {
				// 先删除所对应的 评论 列表
				List<Integer> articleReplyIdList = Db.query("select rid from bbs_reply where aid=?", aid);
				//删除文章的评论
				MyArticleService.me.deleteArticleReplyByIdList(articleReplyIdList);
				
                // 再删除收藏数据
				MyFavoriteService.me.deleteByArticleDeleted(aid);

				// 最后删除 文章
				return Db.update("delete from bbs_article where uid=? and aid=?", accountId, aid) > 0;
			}
		});
		IndexService.me.clearCache();
	}
	
	/**
	 * 根据ID查找文章
	 * @param aid
	 * @return
	 */
	public Article articlefindById(int aid) {
		return articleDao.findById(aid);
	}
	
	/**
	 * 后台修改文章确认
	 * @param article
	 */
	public void articleUpdate(Article article) {
		article.update();
		sendUserMsg(article.getAid());
		IndexService.me.clearCache();
	}	
	
	/** 审核文章给用户发送消息 */
	public void sendUserMsg(Integer aid){
		Article article = articleDao.findById(aid);
		Information information = new Information();
		information.setUid(article.getUid());
		if(article.getStatus()==1){
			information.setTitle("【文章审核】文章通过审核");
			information.setContent("恭喜您的文章【"+article.getTitle()+"】通过审核，审核信息"+article.getReason()+"，再次感谢您的支持。");
		}else if(article.getStatus()==2){
			information.setTitle("【文章审核】文章没有通过审核");
			information.setContent("您的文章【"+article.getTitle()+"】没有通过审核，审核信息"+article.getReason()+"，请重新修改再提交，再次感谢您的支持。");
		}
		information.setCreateAt(new Date());
		information.setType(2);
		information.setIs_see(0);
		information.setAll_user(0);
		information.save();
	}
}